package com.budgetme.widgets;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.budgetme.R;
import com.budgetme.database.Database;
import com.budgetme.graphing.BudgetExpenseGraph;
import com.budgetme.graphing.GraphViewSeries;
import com.budgetme.graphing.GraphViewSeries.GraphViewSeriesStyle;
import com.budgetme.graphing.LineGraphView;
import com.budgetme.graphing.GraphViewSeries.GraphViewData;
import com.budgetme.ui.activities.AddExpenseSimple;


import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.net.Uri;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.TextView;


public class BudgetWidget extends AppWidgetProvider {
	private static final String TAG = BudgetWidget.class.getName();
	
	private static SQLiteDatabase mDatabase;
	
	@Override
	public void onEnabled(Context context) {
		Log.d(TAG, "onEnabled");
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate");
		
		// Perform this loop procedure for each App Widget that belongs to this provider
		for (int appWidgetId : appWidgetIds) {
			Log.d(TAG, "Updating Widget #" + appWidgetId);
			// Tell the AppWidgetManager to perform an update on the current app widget
			updateBudgetWidget(context, appWidgetManager, appWidgetId);
		}
		
	}
	
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        Log.d(TAG, "onDeleted");

        for (int appWidgetId : appWidgetIds) {

        	// Remove the widget entry from shared preferences
        	SharedPreferences.Editor prefs = context.getSharedPreferences(BudgetWidgetConfigure.PREFS_NAME, 0).edit();
        	prefs.remove(BudgetWidgetConfigure.PREFS_KEY_BUCKET_ID + appWidgetId);
        	prefs.commit();
        	
        }
        
    }

	public static void updateBudgetWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
		
		// Get the layout for the App Widget
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.budget_widget_test);
		
		// Look up the bucket id for this widget ID from prefs
		SharedPreferences prefs = context.getSharedPreferences(BudgetWidgetConfigure.PREFS_NAME, 0);
		int id = prefs.getInt(BudgetWidgetConfigure.PREFS_KEY_BUCKET_ID + appWidgetId, 0);
      
		// Get information about the bucket
		ContentResolver mContentResolver = context.getContentResolver();
		
		
		Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
		
        int y = 166;
        int x = 616;
        
		Bitmap bitmap = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		
		Log.e(TAG, "GOT HERE");
		BudgetExpenseGraph graphView = new BudgetExpenseGraph(context, "Title", id);
		graphView.drawGraph(canvas, 166, 616 - 16);
		
		views.setImageViewBitmap(R.id.budget_widget_image, bitmap);
		
		
		
	    // Create an Intent to launch the bucket activity
		Intent bucketUpdaterStarter = new Intent(context, AddExpenseSimple.class);
		bucketUpdaterStarter.putExtra(AddExpenseSimple.ARG_WIDGET_ID, appWidgetId);
		bucketUpdaterStarter.putExtra(AddExpenseSimple.ARG_BUDGET_ID, id);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, bucketUpdaterStarter, PendingIntent.FLAG_UPDATE_CURRENT);
		
		// Attach an on-click listener to the button
		views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
                
        // Tell the widget manager
        appWidgetManager.updateAppWidget(appWidgetId, views);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.layout.budget_widget_test);
        
	}
	
}
